//
//  WBStatusPictureView.swift
//  WB
//
//  Created by macssss on 16/7/7.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {

    
    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    var viewModeL: WBStatusViewModel? {
        didSet {
            calcViewSize()
            
            urls = viewModeL?.picUrls
        }
    }
    
    private func calcViewSize() {
        
        
        if viewModeL?.picUrls?.count == 1 {
            
            let viewSize = viewModeL?.pictureViewSize ?? CGSize()
            
            let v = subviews[0]
            
            v.frame = CGRect(x: 0,
                             y: WBStatusPictureViewOutterMargin,
                             width: viewSize.width,
                             height: viewSize.height - WBStatusPictureViewOutterMargin)
            
        } else {
            
            let v = subviews[0]
            
            v.frame = CGRect(x: 0,
                             y: WBStatusPictureViewOutterMargin,
                             width: WBStatusPictureItemWidth,
                             height: WBStatusPictureItemWidth)
            
        }
        //修改pictureView的高度cons 常数
        heightCons.constant = viewModeL?.pictureViewSize.height ?? 0
        
        
        
    }
    
    
    //配图视图数组
   private var urls: [WBStatusPicture]? {
        
        didSet{
            
            //隐藏所有视图Viwe 
            for  v in subviews {
                
                v.isHidden = true
            }
            //遍历url 数组设置图像
            var index = 0
            for url in urls ?? [] {
                
                let iv = subviews[index] as! UIImageView
                
                if index == 1 && urls?.count == 4 {
                    
                    index += 1
                }
                
                //设置图像
                iv.zs_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                iv.isHidden = false
                
                index += 1
            }
            
        }
        
    }
    
    override func awakeFromNib() {
        
        setupUI()
    }
}


//MARK: -  设置界面
extension WBStatusPictureView {
    
    private func  setupUI() {
        
        self.backgroundColor = UIColor.white()
        
        //超出边界的不显示
        clipsToBounds = true
        
        let count = 3
        
        let rect = CGRect(x: 0,
                          y: WBStatusPictureViewOutterMargin,
                          width: WBStatusPictureItemWidth,
                          height: WBStatusPictureItemWidth)
        
        //循环创建9个ImageView视图
        for i in 0..<count * count {
            
            let iv = UIImageView()
            //等比例显示图像
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            // 行 -> Y
            let row = CGFloat(i / count)
            // 列 -> X
            let col = CGFloat(i % count)
            
            let xOffset = row * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            
            let yOffset = col * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            //每个iv的偏移量
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
            
            
        }
        
        
    }
    
}
